// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var Game = cc.Class({
    extends: cc.Component,

    properties: {
        
        playerPrefab:{
            default:null,
            type: cc.Prefab
        },

        enemyManagerInstance: {
            default: null,
            type: cc.Node
        },

        effectPrefab: {
            default: null,
            type: cc.Prefab
        },

        gameLayet: {
            default: null,
            type: cc.Node
        },

        bgManagerInstance: {
            default: null,
            type: cc.Node
        },

        bulletManagerInstance:{
            default: null,
            type: cc.Node
        },

        scoreLb: {
            default: null,
            type: cc.Label
        },

        menuLayet: {
            default: null,
            type: cc.Node
        },

        btnStartGame: {
            default: null,
            type: cc.Button
        },

        score: 0,
        allScore: 0,
        bulletInfo: null,
    },

    statics: {
        self: null,
        getInstance: function(){
            if (Game.self == null) {
                var node = new cc.Node('Game');
                Game.self = node.addComponent(Game);
            }
            return Game.self;
        }
    },

    drawEnemy(){
        this.enemyManager.drawEnemy();
    },

    drawEffect(node, id, effectName, effectInfo){
        if(node==null) return;
        // 使用给定的模板在场景中生成一个新节点
        var newEffect = cc.instantiate(this.effectPrefab);
        // 将新增的节点添加到 Canvas 节点下面
        if(node.parent != null) node.parent.addChild(newEffect);

        newEffect.setPosition(node.position);

        newEffect.getComponent('effect').initEffect(id, effectName, effectInfo);
    },

    addScore(score){
        this.score += score;
        this.allScore += score;
        this.updateBullet();
        this.setScoreShowInfoLb();
    },

    setScoreShowInfoLb(){
        if (this.scoreLb!= null) {
            this.scoreLb.string = "当前积分：" + this.score + "/" + this.bulletManager.getCurBulletInfo().upScore + "\n总积分：" + this.allScore;
        }
    },

    updateBullet(){
        if(this.bulletManager == null) return;
        this.bulletManager.updateBullet();
    },

    setGameOver()
    {
        this.drawEffect(this.node, 1, 'message1', '游戏结束!!!');
        this.gameOver = true;
        this.score = 0;
        this.allScore = 0;
        this.enemyManager.setGameOver();
        this.bulletManager.setGameOver();
        this.menuLayet.active = true;
    },

    startGame()
    {
        this.gameOver = false;
        this.menuLayet.active = false;
        this.createPlayer();
        this.bulletManager.startGame();
        this.drawEnemy();
        this.setScoreShowInfoLb();
    },

    createPlayer(){
        this.player = cc.instantiate(this.playerPrefab);
        this.player.getComponent('Player').initInfo(this);
        this.gameLayet.addChild(this.player);
        this.player.setPosition(0, - this.node.height / 2);
    },

    // LIFE-CYCLE CALLBACKS:

    ctor(){
        this.scene = new Array();
        Game.self = this;
    },

    onLoad () {
        this.gameOver = true;
        cc.director.getPhysicsManager().enabled = true;
        this.player = null;
        this.enemyManager = this.enemyManagerInstance.getComponent('EnemyManager');
        this.bulletManager = this.bulletManagerInstance.getComponent('BulletManager');
    },



    start () {
        // this.drawEnemy();
    },

    update (dt) {

    },
});
